
Partial Class DashboardControls_Reports
    Inherits System.Web.UI.UserControl
    Implements IDashBoard
    Dim titleLabelString As String = ""
    Dim pageSizeValue As Integer = 0

    Protected Property GridViewSortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.TitleLabel IsNot Nothing Then
                Return TitleLabel.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If TitleLabel IsNot Nothing Then
                TitleLabel.Text = value
            End If
            titleLabelString = value
        End Set
    End Property

    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            Return GridViewSortDirection
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            GridViewSortDirection = value
        End Set
    End Property

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.ReportsGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.ReportsGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not Me.ReportsGridView Is Nothing Then
                Return Me.ReportsGridView.PageSize
            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not Me.ReportsGridView Is Nothing Then
                Me.ReportsGridView.PageSize = value
                Me.NumberOfRowsDropDownList.SelectedValue = value.ToString
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                Return ""
            Else
                Return ViewState("SortField")
            End If
            Return Me.ReportsGridView.SortExpression
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged

    End Sub


    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load


        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.TitleLabel.Text = titleLabelString
        End If

        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If


        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "ReportShortName", WebControls.SortDirection.Ascending)
            Me.BindGrid()
        End If


        

    End Sub

    Public Sub SetScreenPositioning(ByVal displayRowsLeftPosition As Integer, _
                                    ByVal rightCornerLeftPosition As Integer, _
                                    ByVal headerWidth As Integer, _
                                    ByVal gridWidth As Integer)

        Dim usercontrolwidth As Integer = rightCornerLeftPosition + 9
        Dim middlecolumnwidth As Integer = usercontrolwidth - 18
        Dim displayspantabledata As Integer = displayRowsLeftPosition - 9

        Me.HeaderDiv.Style("width") = usercontrolwidth.ToString() & "px"
        Me.UserControlTable.Style("width") = usercontrolwidth.ToString() & "px"
        Me.GridTable.Style("width") = usercontrolwidth.ToString() & "px"
        Me.MiddleColumnTableData.Style("width") = middlecolumnwidth.ToString() & "px"
        Me.DisplaySpanTableData.Style("width") = (displayRowsLeftPosition - 9).ToString() & "px"
        Me.DisplayRowsTableData.Style("width") = (middlecolumnwidth - displayspantabledata).ToString() & "px"
        Me.ReportsGridView.Width = Unit.Pixel(usercontrolwidth)

    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                                ByVal e As System.EventArgs) _
                                                                Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.ReportsGridView.AllowPaging = False
        Else
            Me.ReportsGridView.AllowPaging = True
            Me.ReportsGridView.PageSize = NumberOfRowsDropDownList.SelectedValue
            Me.ReportsGridView.PageIndex = 0
        End If

        Me.BindGrid()

    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid

        Dim isAscending As Boolean = Me.SortDirection.Equals(SortDirection.Ascending)
        Me.ReportsGridView.DataSource = Cdm.DataCore.CaseNotes.Reports.ListByRoleId(UserAuthorization.CurrentUser.RoleId, Me.SortField, isAscending)
        Me.ReportsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)
    End Sub

    Protected Sub ReportsGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReportsGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(ReportsGridView)
    End Sub

    Protected Sub ReportsGridView_PageIndexChanging(ByVal sender As Object, _
                                                    ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                    Handles ReportsGridView.PageIndexChanging

        Me.PageNumber = e.NewPageIndex
        Me.BindGrid()

    End Sub

    Public Sub LoadControl1(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Implements IDashBoard.LoadControl

        Me.Page_Load(sender, e)

    End Sub

    Public Function DisplayNavigateURL(ByVal reportName As String) As String
        Try

            Return "~/Reports/" & reportName

        Catch ex As Exception
            Cdm.AppServices.Log.ErrorLog("DashboardControls_Reports", "An error occurred in the DisplayNavigateURL() method.", ex)
            Throw ex
            Return ""
        End Try
    End Function

    Protected Sub ReportsGridView_Sorting(ByVal sender As Object, _
                                          ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
                                          Handles ReportsGridView.Sorting

        If GridViewSortDirection = SortDirection.Ascending And _
            e.SortExpression.Equals(Me.SortField) Then
            GridViewSortDirection = SortDirection.Descending
            Me.ReportsGridView.DataSource = Cdm.DataCore.CaseNotes.Reports.ListByRoleId(UserAuthorization.CurrentUser.RoleId, e.SortExpression, False)
        Else
            GridViewSortDirection = SortDirection.Ascending
            Me.ReportsGridView.DataSource = Cdm.DataCore.CaseNotes.Reports.ListByRoleId(UserAuthorization.CurrentUser.RoleId, e.SortExpression, True)
        End If

        If Me.NumberOfRowsDropDownList.SelectedValue <> -1 Then
            Me.ReportsGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
        End If

        Me.SortField = e.SortExpression
        Me.ReportsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)

    End Sub

End Class
